/*
 * Copyright (c) 2020, EMERGETECH Company Ltd. All rights reserved
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors
 * may be used to endorse or promote products derived from this software without
 * specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef __VPECODEC__
#define __VPECODEC__

#ifdef __cplusplus
extern "C" {
#endif

#include "vpi_api.h"
#include "vpi_types.h"

typedef struct VpeOption {
    char *key;
    char *value;
} VpeOption;

typedef struct VpeDeviceContext {
    int device;
    VpiApi *func;
    VpiSysInfo *vpi_sys;
} VpeDeviceContext;

typedef struct VpeHwDwCtx
{
    VpiCtx ctx;
    VpiApi *vpi;
    int initialized;
}VpeHwDwCtx;

typedef struct VpeHwUlCtx {
    VpiCtx ctx;
    VpiApi *vpi;
    int initialized;
    int alignment;
} VpeHwUlCtx;

typedef struct VpePPCtx {
    VpiCtx ctx;
    VpiApi *vpi;

    int nb_outputs;
    int force_10bit;
    char *low_res;
    VpiPPOption *option;
    int initialized;
} VpePPCtx;

typedef struct VpeVideoInfo {
    int width;
    int height;
    char *src_pix_fmt;
    VpiPixsFmt fmt;
    uint8_t *data[3];
    int size[3];
    int linesize[3];
} VpeVideoInfo;

typedef struct VpeEncCtx {
    VpiCtx ctx;
    VpiApi *vpi;
    VpiFrame *vpi_frame;
    VpiEncParamSet *param_list;
    int eof;
    char *preset;
    char *profile;
    char *level;
    char *enc_params;
    int crf;
    char *force_idr;
    int effort;
    int lag_in_frames;
    int passes;
    int bit_rate;
    void *enc_cfg;
    int initialized;
    unsigned int color_range;
    unsigned int transfer_characteristics;/*VUI param*/
    unsigned int matrix_coefficients;/*VUI param*/
    unsigned int colour_primaries;/*VUI param*/
} VpeEncCtx;

typedef struct {
     VpiSysInfo *vpi_sys_info;
     int device_index;
     int device;  //open device handle
     VpiApi *func;
     char device_name[256];
     pthread_mutex_t mutex;
     int ref;
} hwcontext_device;
typedef struct {
    int width;
    int height;
    int fps;
    int resource_type;
    float total_consume_resource;
    pid_t pid;
    int device_index;
    hwcontext_device* hw_device;
    int ext_device_en;
    int ref;
}device_handle;

typedef struct {
    int device_id;    //device index from 0 starting, /dev/transcoder0
    int power_state;
    int dec_usage;    //dec usage percentage
    int enc_usage;    //enc usage percentage 
    int used_mem;     //used mem s0+s1 total
    int free_mem;     //free mem total_mem-used_mem
    int tasks;        //total tasks
    float dec_720p30;
    float enc_720p30;
} SrmDriverStatus;

typedef struct {
   SrmDriverStatus *driver_status;
   int driver_nums;
} SrmContext;

typedef struct VpeContext
{
    VpiCtx vpi_ctx;
    VpiApi *vpi;
    VpiPacket *vpi_packet;
    VpiPlugin *vpi_plugin;
    VpiSysInfo *vpi_sys_info;
    VpiFrame *frame_ctx;
    VpiHwDwFrameInfo *hwdl_frame_info;
    VpiPicInfo *pic_info;
    //self definition
    VpeDeviceContext device_ctx;
    VpeHwDwCtx hwdl_ctx;
    VpeEncCtx enc_ctx;
    VpePPCtx pp_ctx;
    VpeHwUlCtx hwul_ctx;
    VpeVideoInfo video_info;
    device_handle *hw_device;
    int index;
    int dec_finished;
    uint8_t *pkt_data;
    int      pkt_max_size;
    int64_t pre_pts;
    int input_rate_denom;
    int input_rate_numer;
} VpeContext;

#ifdef __cplusplus
}
#endif

#endif
